package com.palnar.ilecker.ui.waiter.dashBoard;

import java.util.ArrayList;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;

import com.palnar.ilecker.R;
import com.palnar.ilecker.adapter.TableGridAdapter;
import com.palnar.ilecker.baseactivity.BaseActivity;
import com.palnar.ilecker.model.adapterModels.WaiterTableModel;

public class WaiterMenuActivity extends BaseActivity implements OnClickListener {

	private Context context;

	private Intent i;

	private GridView gridTables;
	private Button btnAddTable;

	private TableGridAdapter tableGridAdapter;

	private PopupWindow changeSortPopUp;

	private ArrayList<WaiterTableModel> waiterTableList;
	
	 int color=R.color.color1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_waiter_dashboard);
		context = this;
		setupAd();
		intializeUI();
		setListners();
		setSelectionManager();

	}

	private void intializeUI() {

		// TODO Auto-generated method stub
		gridTables = (GridView) findViewById(R.id.gridTables);

		btnAddTable = (Button) findViewById(R.id.btnAddTable);

		waiterTableList = new ArrayList<WaiterTableModel>();

		/*
		 * waiterTableList.add(new WaiterTableModel(new String[]{"T1"}, new
		 * int[]{R.color.color1})); waiterTableList.add(new WaiterTableModel(new
		 * String[]{"T1","T2"}, new int[]{R.color.color1,R.color.color2}));
		 * waiterTableList.add(new WaiterTableModel(new
		 * String[]{"T1","T2","T3"}, new
		 * int[]{R.color.color1,R.color.color2,R.color.color3}));
		 * waiterTableList.add(new WaiterTableModel(new
		 * String[]{"T1","T2","T3","T4"}, new
		 * int[]{R.color.color1,R.color.color2,R.color.color3,R.color.color4}));
		 * waiterTableList.add(new WaiterTableModel(new
		 * String[]{"T1","T2","T3","T4","T5"}, new
		 * int[]{R.color.color1,R.color.color2
		 * ,R.color.color3,R.color.color4,R.color.color5}));
		 * waiterTableList.add(new WaiterTableModel(new String[]{"T1"}, new
		 * int[]{R.color.color1}));
		 * 
		 * waiterTableList.add(new WaiterTableModel(new String[]{"T1"}, new
		 * int[]{R.color.color1})); waiterTableList.add(new WaiterTableModel(new
		 * String[]{"T1","T2"}, new int[]{R.color.color1,R.color.color2}));
		 * waiterTableList.add(new WaiterTableModel(new
		 * String[]{"T1","T2","T3"}, new
		 * int[]{R.color.color1,R.color.color2,R.color.color3}));
		 * waiterTableList.add(new WaiterTableModel(new String[]{"T1","T2"}, new
		 * int[]{R.color.color1,R.color.color2})); waiterTableList.add(new
		 * WaiterTableModel(new String[]{"T1","T2","T3"}, new
		 * int[]{R.color.color1,R.color.color2,R.color.color3}));
		 * waiterTableList.add(new WaiterTableModel(new String[]{"T1"}, new
		 * int[]{R.color.color2})); waiterTableList.add(new WaiterTableModel(new
		 * String[]{"T1","T2","T3","T4","T5"}, new
		 * int[]{R.color.color1,R.color.color2
		 * ,R.color.color3,R.color.color4,R.color.color5}));
		 * waiterTableList.add(new WaiterTableModel(new String[]{"T1","T2"}, new
		 * int[]{R.color.color1,R.color.color2})); waiterTableList.add(new
		 * WaiterTableModel(new String[]{"T1","T2","T3"}, new
		 * int[]{R.color.color5,R.color.color2,R.color.color3}));
		 * waiterTableList.add(new WaiterTableModel(new String[]{"T1","T2"}, new
		 * int[]{R.color.color1,R.color.color2})); waiterTableList.add(new
		 * WaiterTableModel(new String[]{"T1","T2","T3"}, new
		 * int[]{R.color.color1,R.color.color2,R.color.color3}));
		 * waiterTableList.add(new WaiterTableModel(new String[]{"T1"}, new
		 * int[]{R.color.color3})); waiterTableList.add(new WaiterTableModel(new
		 * String[]{"T1","T2","T3","T4","T5"}, new
		 * int[]{R.color.color1,R.color.color5
		 * ,R.color.color3,R.color.color4,R.color.color2}));
		 * waiterTableList.add(new WaiterTableModel(new String[]{"T1","T2"}, new
		 * int[]{R.color.color1,R.color.color2})); waiterTableList.add(new
		 * WaiterTableModel(new String[]{"T1","T2","T3"}, new
		 * int[]{R.color.color6,R.color.color2,R.color.color3}));
		 * waiterTableList.add(new WaiterTableModel(new String[]{"T1","T2"}, new
		 * int[]{R.color.color1,R.color.color2})); waiterTableList.add(new
		 * WaiterTableModel(new String[]{"T1","T2","T3"}, new
		 * int[]{R.color.color1,R.color.color2,R.color.color3}));
		 * waiterTableList.add(new WaiterTableModel(new String[]{"T1"}, new
		 * int[]{R.color.color4})); waiterTableList.add(new WaiterTableModel(new
		 * String[]{"T1","T2"}, new int[]{R.color.color6,R.color.color2}));
		 * waiterTableList.add(new WaiterTableModel(new
		 * String[]{"T1","T2","T3"}, new
		 * int[]{R.color.color5,R.color.color2,R.color.color3}));
		 * waiterTableList.add(new WaiterTableModel(new String[]{"T1","T2"}, new
		 * int[]{R.color.color1,R.color.color2})); waiterTableList.add(new
		 * WaiterTableModel(new String[]{"T1","T2","T3","T4","T5"}, new
		 * int[]{R.color
		 * .color5,R.color.color2,R.color.color3,R.color.color4,R.color
		 * .color6})); waiterTableList.add(new WaiterTableModel(new
		 * String[]{"T1","T2","T3"}, new
		 * int[]{R.color.color1,R.color.color2,R.color.color3}));
		 */

		tableGridAdapter = new TableGridAdapter(context, 0, waiterTableList, 3);
		gridTables.setNumColumns(3);
		gridTables.setAdapter(tableGridAdapter);
	}

	private void setListners() {
		btnAddTable.setOnClickListener(this);

	}

	private void setSelectionManager() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View v) {
		if (v == btnAddTable) {
			showTableSelectionPopup(context);
		}
	}

	public void showTableSelectionPopup(final Context activity) {
		
		
		 color=R.color.color1;

		LayoutInflater inflater = (LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater
				.inflate(R.layout.popup_tableselection, null, false);

		final PopupWindow pw = new PopupWindow(view, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT, true);

		final String tableString[] = { "T1", "T2", "T3", "T4", "T5" };

		final Spinner spinnerTables = (Spinner) view
				.findViewById(R.id.spinnerTables);

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				WaiterMenuActivity.this, android.R.layout.simple_spinner_item,
				tableString);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerTables.setAdapter(adapter);

		RadioGroup radioGroupColors = (RadioGroup) view
				.findViewById(R.id.radioGroupColors);

		radioGroupColors
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup arg0, int id) {

						if (id == R.id.radio1) {

							color=R.color.color1;
							
						} else if (id == R.id.radio2) {

							  color=R.color.color2;
							
						} else if (id == R.id.radio3) {
							  color=R.color.color3;
							
						} else if (id == R.id.radio4) {

							  color=R.color.color4;
						} else if (id == R.id.radio5) {

							  color=R.color.color5;
						} else if (id == R.id.radio6) {

							  color=R.color.color6;
						}

					}
				});

		Button btnOK = (Button) view.findViewById(R.id.btnOK);

		btnOK.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				pw.dismiss();

				String s[] = { ""
						+ tableString[spinnerTables.getSelectedItemPosition()] };

				waiterTableList.add(new WaiterTableModel(
						new String[] { tableString[spinnerTables
								.getSelectedItemPosition()] },
						new int[] { color}));

				tableGridAdapter = new TableGridAdapter(context, 0,
						waiterTableList, 3);

				gridTables.setAdapter(tableGridAdapter);

			}
		});

		Button btnCancel = (Button) view.findViewById(R.id.btnCancel);

		btnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				pw.dismiss();

			}
		});

		pw.showAtLocation(view, Gravity.CENTER, 0, 0);

	}

}
